package com.hll.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hll.erp.entity.Product;
import com.hll.erp.entity.ResultMap;
import com.hll.erp.service.CategoryService;
import com.hll.erp.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Product)表控制层
 *
 * @author makejava
 * @since 2021-07-04 15:17:56
 */
@RestController
@RequestMapping("product")
@Api(
    value = "Product Controller",
    tags = {"产品信息接口"})
public class ProductController {
  /** 服务对象 */
  @Autowired private ProductService productService;

  @Autowired private CategoryService categoryService;

  @RequiresRoles(value = "product")
  @ApiOperation(value = "获取产品列表,分页方式")
  @PostMapping("/getProducts")
  public ResultMap getProducts(
      @RequestParam(defaultValue = "1", value = "pageNo") Integer pageNo,
      @RequestParam(defaultValue = "5", value = "pageSize") Integer pageSize,
      @RequestParam(value = "name", required = false) String name,
      @RequestParam(value = "category", required = false) Integer category) {
    Page<Product> products = productService.getProducts(pageNo, pageSize, name, category);
    return new ResultMap().success().code(200).message((products));
  }

  @RequiresRoles(
      value = {"product", "buyer", "seller"},
      logical = Logical.OR)
  @ApiOperation(value = "获取名字相似的产品列表")
  @PostMapping("/getProductsByName")
  public ResultMap getProductsByName(
      @RequestParam(defaultValue = "", required = false) String name) {
    List<Product> products = productService.list(new QueryWrapper<Product>().like("pname", name));
    return new ResultMap().success().code(200).message(products);
  }

  @RequiresRoles(value = "product")
  @ApiOperation(value = "更改产品信息")
  @PostMapping("/modifyProductInfo")
  public ResultMap modifyInfo(@RequestBody Product product) {
    ResultMap resultMap = new ResultMap();
    boolean flag = productService.getById(product.getPid()) != null;
    if (flag) {
      flag =
          product.getNumber() > -1
              && categoryService.getById(product.getCategory()) != null
              && ((productService.getProductByName(product.getPname()) == null)
                  || (productService
                      .getProductByName(product.getPname())
                      .getPid()
                      .equals(product.getPid())));
      if (flag && productService.updateById(product)) {
        resultMap.success().code(200).message("更新成功");
      } else {
        resultMap.fail().code(201).message("更新失败,数据库插入失败 / number<0 / 类别不存在 / 产品名已存在");
      }
    } else {
      resultMap.fail().code(207).message("product不存在");
    }
    return resultMap;
  }

  @RequiresRoles(
      value = {"product", "sell_watch"},
      logical = Logical.OR)
  @PostMapping("/getProductById")
  public ResultMap getProductById(@RequestParam String pid) {
    Product product = productService.getById(pid);
    return new ResultMap().success().code(200).message(product);
  }

  @RequiresRoles(value = "admin")
  @ApiOperation(value = "添加产品")
  @PostMapping("/addProduct")
  public ResultMap addProduct(@RequestBody Product product) {
    ResultMap resultMap = new ResultMap();
    if (productService.getProductByName(product.getPname()) != null) {
      resultMap.fail().code(203).message("产品已存在");
    } else if (product.getNumber() < 0) {
      resultMap.fail().code(201).message("数量需大于0");
    } else if (categoryService.getById(product.getCategory()) == null) {
      resultMap.fail().code(201).message("类别不存在");
    } else {
      if (productService.save(product)) {
        resultMap.success().code(200).message("插入成功");
      } else {
        resultMap.fail().code(201).message("插入数据库失败");
      }
    }
    return resultMap;
  }

  @PostMapping("/getProductCount")
  public ResultMap getProductCount() {
    return new ResultMap().success().code(200).message(productService.count());
  }
}
